#include <pthread.h>
#include <iostream>
#include <string>
#include <vector>
#include <unistd.h>
#include "ThreadData.hpp"

using std::cout;
using std::endl;

const int threadsnum = 6;

using namespace zzz;

void *ThreadHandle(void *tdata)
{
    ThreadData *td = static_cast<ThreadData *>(tdata);

    int retval = td->Excute();

    ThreadResult *ret = new ThreadResult(retval, td->GetName());

    delete td;
    return ret;
}

int main()
{
    std::vector<pthread_t> threads;
    std::vector<ThreadResult *> thread_rets;

    for (int i = 0; i < threadsnum; i++)
    {
        // 有需要线程处理的数据
        char threadsname[32];
        snprintf(threadsname, 32, "线程 - %d", i + 1);

        ThreadData *tdata = new ThreadData(i + 1, i + 1, (std::string)threadsname);

        // 创建一个新线程
        pthread_t tid;
        pthread_create(&tid, nullptr, ThreadHandle, tdata);

        threads.push_back(tid);
    }

    for (auto &tid : threads)
    {
        void *ret = nullptr;
        pthread_join(tid, &ret);

        thread_rets.push_back((ThreadResult *)ret);
    }
    for (auto ret : thread_rets)
    {
        ret->Print();
        delete ret;
    }
    return 0;
}
